package com.example.scheduled.service;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author ljw
 * @description
 * @date 2021/8/11 8:00
 */
@Service
public class ScheduledTaskService {

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @Scheduled(fixedRate = 5000)
    public void fixedRateTime() throws InterruptedException {
        System.out.println("每隔五秒执行一次：" + dateFormat.format(new Date()));
        Thread.sleep(4000);
    }

    @Scheduled(fixedDelay = 3000)
    public void fixedDelayTime() throws InterruptedException {
        System.out.println("延迟3秒执行：" + dateFormat.format(new Date()));
        Thread.sleep(2000);
    }

    @Scheduled(cron = "0 14 8 ? * *")
    public void cronTime() throws InterruptedException {
        System.out.println("在指定时间执行：" + dateFormat.format(new Date()));
    }
}
